package com.acc.service.impl;

import java.util.List;

import com.acc.dao.AuthorityDAO;
import com.acc.dao.impl.AuthorityDAOImpl;
import com.acc.entity.AdminRole;
import com.acc.service.AuthorityService;
/**
 * 
 * @author 朱春阳
 *
 */
public class AuthorityServiceImpl implements AuthorityService{

	private AuthorityDAO authorityDao = new AuthorityDAOImpl();
	/**
	 * 根据条件获取对应的管理员---角色关系表
	 */
	@Override
	public List<AdminRole> getAllAdminRole() {
		// TODO Auto-generated method stub
		String sql = "select * from admin_role";
		List<AdminRole> adminroles = authorityDao.getAllAdminRole(sql);
		return adminroles;
	}
	
	/**
	 * 分页查询管理员---角色关系表
	 */
	@Override
	public List<AdminRole> getPageRole(Integer page, Integer size) {
		// TODO Auto-generated method stub
		String sql = "select * from (select admin_role.*, rownum num from admin_role where rownum <= ?) t where t.num > ?";
		List<AdminRole> adminroles = authorityDao.getAllAdminRole(sql, page * size, (page-1)*size);
	
		return adminroles;
	}
	
	/**
	 * 通过管理员名字和角色删除对应的条目
	 */
	@Override
	public boolean deleteAdminRole(String adminName, String adminRole) {
		// TODO Auto-generated method stub
		String sql = "delete from admin_role where admin_name = ? and admin_role = ?";
		Integer num = authorityDao.deleteOneAdminRole(sql, adminName, adminRole);
		if(num != null) {
			return true;
		}else {
			return false;
		}
		
	}

	/**
	 * 新增 ADMIN-ROLE 对应关系
	 */
	@Override
	public boolean addAdminRole(AdminRole adminrole) {
		// TODO Auto-generated method stub
		String sql = "insert into admin_role values(seq_admin_role_id.nextval ,?,?)";
		Integer num = authorityDao.addOneAdminRole(sql, adminrole.getAdminName(), adminrole.getAdminRole());
		return (num != null);
	}

	/**
	 * 更新ADMIN-ROLE对应关系
	 */
	@Override
	public boolean updateAdminRole(String adminName, String adminRole, Integer id) {
		// TODO Auto-generated method stub
		String sql = "update admin_role set admin_name = ? and admin_role = ? where role_id=?";
		Integer num = authorityDao.updateOneAdminRole(sql, adminName, adminRole, id);
		return num != null;
	}

}
